package com.floss.preloading.machinist.library
{
	
	import com.crl.core.AssetTypes;
	
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.media.Sound;
	

	public class AssetLibrary
	{	
		private var bitmaps:Array;
		private var bitmapsByGroup:Array;
		private var swfs:Array;
		private var byteSwfs:Array;
		private var sounds:Array;
		private var progressiveFLVS:Array;
		private var fzips:Array;
		private var xmls:Array;
		
		public function AssetLibrary()
		{
			bitmaps = [];
			bitmapsByGroup = [];
			swfs = [];
			byteSwfs = [];
			sounds = [];
			progressiveFLVS = [];
			fzips = [];
			xmls = [];
		}
		
		/**
		 * Register an asset in the library.
		 * @param	String		The asset type.
		 * @param	String		The asset identifier
		 * @param	*			The loaded asset object
		 * @param	group		(optional) If the type is a bitmap(jpg,png,gif). Then a group can optionally be specified.
		 */
		public function registerItem(type:String,identifier:String,obj:*,group:String = null):void
		{
			switch(type)
			{
				case AssetTypes.XML:
					xmls[identifier] = obj;
					break;
				case AssetTypes.BITMAP:
					bitmaps[identifier] = obj;
					if(group)
					{
						if(bitmapsByGroup[group] == null){
							bitmapsByGroup[group] = [];
						}
						bitmapsByGroup[group].push(obj);
					}
					break;
				case AssetTypes.SOUND:
					sounds[identifier] = obj;
					break;
				case AssetTypes.BYTE_SWF:
					byteSwfs[identifier] = obj;
					break;
				case AssetTypes.SWF:
					swfs[identifier] = obj;
					break;
				case AssetTypes.PROGRESSIVE_FLV:
					progressiveFLVS[identifier] = obj;
					break;
				case AssetTypes.FZIP:
					fzips[identifier] = obj;
					break;
			}
		}
		
		/**
		 * Get an entire byte swf that was loaded.
		 * @param	String		The identifier that was used to register the byte swf in the asset library.
		
		public function getByteSWF(identifier:String, zipFileContainer:String = null):URLLoader
		{
			if(zipFileContainer)
			{
				if(!fzips[identifier])
				{
					throw new Error("The zipfile {" + zipFileContainer + "} was not found");
				}
				else
				{
					var file:FZipFile = fzips[zipFileContainer].getFileByName(identifier);
					var content:* = initializer.initializeContentFromByteArray(AssetTypes.FZIP_FILE,file.content);
					var finalByteSWF:URLLoader = initializer.initializeContentFromByteArray(AssetTypes.BYTE_SWF,content);
					return finalByteSWF;
				}
			}
			else
			{
				if(byteSwfs[identifier] != null)
					return byteSwfs[identifier] as URLLoader;
			}
			throw new Error("The byteswf {" + identifier + "} was not found");
		}
		 */
		
		/**
		 * Get an asset from a byte swf file.
		 */
		public function getAssetFromByteSWF(identifier:String, classNameInLibrary:String, zipFileContainer:String = null):DisplayObject
		{
			if(zipFileContainer != null)
			{
			}
			else
			{
				if(byteSwfs[identifier] != null)
				{
					var swf:Loader = byteSwfs[identifier] as Loader;
					var SymbolClassMC:Class = swf.contentLoaderInfo.applicationDomain.getDefinition(classNameInLibrary) as Class;
					var symbolInstance:DisplayObject = new SymbolClassMC() as DisplayObject;	
					return symbolInstance;
				}
			}
			
			return null;
		}
		
		/**
		 * Get a moie as a movie clip.
		 */
		public function getAssetFromByteSWFAsMovieClip(identifier:String, classNameInLibrary:String, zipFileContainer:String = null):MovieClip
		{
			var dis:DisplayObject=getAssetFromByteSWF(identifier,classNameInLibrary,zipFileContainer);
			if(dis !=null)return dis as MovieClip;
			
			return null;
		}
		
		/**
		 * Get a regular swf that was loaded.
		 * @param		String		The identifier that was used to register the item in the library.
		 */
		public function getSWF(identifier:String, zipFileContainer:String = null):Loader
		{
			if(zipFileContainer)
			{
					
			}
			else
			{
				if(swfs[identifier] != null)
					return byteSwfs[identifier] as Loader;
			}
			
			return null;
		}
		
		/**
		 * Get a bitmap that was loaded.
		 */
		public function getBitmap(identifier:String, zipFileContainer:String = null):Loader
		{
			if(zipFileContainer)
			{
				if(!fzips[identifier])
				{
					throw new Error("The zipfile {" + zipFileContainer + "} was not found");
				}
				else
				{
					var file:FZipFile = fzips[zipFileContainer].getFileByName(identifier);
					var content:* = initializer.initializeContentFromByteArray(AssetTypes.FZIP_FILE,file.content);
					var finalBitmap:Loader = initializer.initializeContentFromByteArray(AssetTypes.BITMAP,content);
					return finalBitmap;
				}
			}
			else
			{
				if(bitmaps[identifier] != null){
					return byteSwfs[identifier] as Loader;
				}
			}
			
			return null;
		}
		
		/**
		 * Get a sound asset that was loaded.
		 */
		public function getSound(identifier:String, zipFileContainer:String = null):Sound
		{
			if(zipFileContainer)
			{
				if(fzips[identifier]=null)
				{
					var file:FZipFile = fzips[zipFileContainer].getFileByName(identifier);
					var content:* = initializer.initializeContentFromByteArray(AssetTypes.FZIP_FILE,file.content);
					var finalSound:Sound = initializer.initializeContentFromByteArray(AssetTypes.SOUND,content);
					return finalSound;
				}	
			}
			else
			{
				if(sounds[identifier] != null){
					return sounds[identifier] as Sound;
				}
			}
			return null;
		}
		
		/**
		 * Get's a FZip that was registered.
		 * @param	String		The identifier used in registering the zip.
		 */
		public function getFZip(identifier:String):Loader
		{
			if(fzips[identifier] != null){
				return fzips[identifier] as Loader;
			}
			return null;
		}
	}
}